package com.view
{	
	
	import caurina.transitions.Tweener;
	
	import com.events.AwardEvent;
	import com.util.TextLayout;
	
	import flash.display.Bitmap;
	import flash.display.BlendMode;
	import flash.display.GradientType;
	import flash.display.Shape;
	import flash.display.SpreadMethod;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.filters.BitmapFilterQuality;
	import flash.filters.BlurFilter;
	import flash.geom.Matrix;
	import flash.text.TextField;
	import flash.text.TextFormat;
	
	import sekati.display.Canvas;
	import sekati.display.CoreSprite;

	/**draw the main stage, all the elements is on this stage */
	public class mainStage extends CoreSprite
	{	
		private var stageBg:Bitmap=new Bitmap();
		private var stageBgMain:Sprite=new Sprite();
		private var bggrey:Shape;
		private var bgshadow:Shape;
		public var messageBox:TextField=new TextField();

		
		public function mainStage()
		{
			super();				
			/** annouce event listener*/
			Canvas.stage.addEventListener(AwardEvent.BACKGROUDBLUR,backgroudblur);
			Canvas.stage.addEventListener(AwardEvent.BACKGROUDCLEAR,backgroudclear);
			Canvas.stage.addEventListener(MouseEvent.MOUSE_MOVE,onStageMouseMove);
		}
		/**set function to set StageBg */
		public function set _stageBg(bg:Bitmap):void{
			stageBg=bg;
		}
		/**set function to set StageMain */
		public function set _stageBgMain(bgm:Bitmap):void{
			stageBgMain.addChild(bgm);
		}
		/** after the stage element loaded, add them into stage */
		public function drawStage():void{
			drawBackground();			
			drawShawdow();
			drawbgMain();	
			drawMessageBox();		
		}
		
		/** set the bg image into stage */
		private function drawBackground():void{
			 bggrey =new Shape();
			 bggrey.graphics.beginBitmapFill(stageBg.bitmapData);
			 bggrey.graphics.drawRect(0,0,Canvas.stage.stageWidth,Canvas.stage.stageHeight);
			 bggrey.graphics.endFill();
			 bggrey.alpha=0.8;
			 addChild(bggrey);
		}

		
		/** draw the shawdow into stage */
		private function drawShawdow():void{
			bgshadow = new Shape();
			var fillType:String = GradientType.LINEAR;
			var colors:Array = [0x51504c, 0x555555,0x000000]; 
			var alphas:Array = [100, 0 ,100];
			var ratios:Array = [0x00, 0x8f,0xff];
			var matr:Matrix = new Matrix();
			matr.createGradientBox(Canvas.stage.stageWidth, Canvas.stage.stageHeight, Math.PI/2, 0, 0);
			var spreadMethod:String = SpreadMethod.PAD;
			bgshadow.graphics.beginGradientFill(fillType,colors,alphas,ratios,matr,spreadMethod);
			bgshadow.graphics.drawRect(0,0,Canvas.stage.stageWidth,Canvas.stage.stageHeight);
			bgshadow.graphics.endFill();
			addChild(bgshadow);
			
			/*
			var bgshadow1:Shape= new Shape();
			matr.createGradientBox(Canvas.stage.stageWidth, Canvas.stage.stageHeight, 0, 0, 0);
			bgshadow1.graphics.beginGradientFill(fillType,colors,alphas,ratios,matr,spreadMethod);
			bgshadow1.graphics.drawRect(0,0,Canvas.stage.stageWidth,Canvas.stage.stageHeight);
			bgshadow1.graphics.endFill();*/
			//addChild(bgshadow1);			
		}
		
		/** draw the main element into stage */
		private function drawbgMain():void{
			stageBgMain.blendMode=BlendMode.SCREEN;
			stageBgMain.alpha=0;
			stageBgMain.scaleX=0.8;
			stageBgMain.scaleY=0.8;
			stageBgMain.x=Canvas.stage.stageWidth/2-stageBgMain.width/2;
			stageBgMain.y=Canvas.stage.stageHeight/2-stageBgMain.height/2+stageBgMain.height/8;
			addChild(stageBgMain);
			Tweener.addTween(stageBgMain,{alpha:0.8, time:10, transition:"easeOutExpo"});			
		}
		private function redrawbgMain():void{
			stageBgMain.x=Canvas.stage.stageWidth/2-stageBgMain.width/2;
			stageBgMain.y=Canvas.stage.stageHeight/2-stageBgMain.height/2+stageBgMain.height/8;
			addChild(stageBgMain);			
		}
		private function drawMessageBox():void{	
			messageBox.text="Loading";
			messageBox.setTextFormat(new TextFormat("Arial",12,0xffffff,null));
			addChild(messageBox);
		}

		public function resize():void{
			if(bgshadow!=null){
				empty();
			}
			drawBackground();			
			drawShawdow();
			redrawbgMain();
		}
		private function empty():void{
			//removeChild(stageBgMain);
			removeChild(bgshadow);
			removeChild(bggrey);			
		}
		private function backgroudblur(e:Event):void{
			trace("Stage is blur");			
			var blur:BlurFilter = new BlurFilter();
			blur.blurX = 50;
			blur.blurY = 50;
			blur.quality = BitmapFilterQuality.MEDIUM;		
			stageBgMain.filters=[blur];
		}
		private function backgroudclear(e:Event):void{
			trace("Stage is clear");
			stageBgMain.filters=[];
		}
		private function onStageMouseMove(e:MouseEvent):void{
			//dispatchEvent
			Message("("+mouseX+","+mouseY+")");
		}
		public function Message(mess:String):void{	
			if(messageBox!=null){
				messageBox.text=mess;
			}			
		}
		
	}
}